/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mytdev.application.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Yann D'Isanto
 */
public class ApplicationServiceManager {

    private ApplicationServiceLoader serviceLoader;
    private Map<String, Object> servicesCache = new HashMap<String, Object>();

    public ApplicationServiceManager(ApplicationServiceLoader serviceLoader) {
        this.serviceLoader = serviceLoader;
    }

    public String[] getAvailableServices() {
        return serviceLoader.getAvailableServices();
    }

    public synchronized Object getService(String name) throws ServiceException {
        Object service = servicesCache.get(name);
        if (service == null) {service = loadService(name);
        }
        return service;
    }

    public synchronized <T> T getService(String name, Class<T> serviceType) throws ServiceException {
        T service = null;
        try {
            service = (T) servicesCache.get(name);
        } catch(ClassCastException ex) {
            throw new ServiceException("invalid service type", ex);
        }
        if (service == null) {
            service = loadService(serviceType, name);
        }
        return service;
    }

    public boolean isServiceToLoadOnStartUp(String name) {
        return serviceLoader.getServiceDescriptor(name).isToLoadOnStartUp();
    }

    public Object loadService(String name) throws ServiceException {
//        List<String> dependencies = serviceLoader.getServiceDependencies(name);
//        for (String dependency : dependencies) {
//            getService(dependency);
//        }
//        Object service = serviceLoader.getService(name);
//        if (service != null && serviceLoader.getServiceDescriptor(name).isCacheable()) {
//            servicesCache.put(name, service);
//        }
//        return service;
        return loadService(Object.class, name);
    }

    public <T> T loadService(Class<T> serviceType, String name) throws ServiceException {
        List<String> dependencies = serviceLoader.getServiceDependencies(name);
        for (String dependency : dependencies) {
            getService(dependency);
        }
        T service = serviceLoader.getService(serviceType, name);
        if (service != null && serviceLoader.getServiceDescriptor(name).isCacheable()) {
            servicesCache.put(name, service);
        }
        return service;
    }
}
